package com.dminvoicemanager.tabs;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import com.dminvoicemanager.DM_InvoiceMenu;
import com.dminvoicemanager.DM_InvoiceSalesOrder;
import com.dminvoicemanager.DM_List_Expand_Helper;
import com.dminvoicemanager.DM_Retailer_Master;
import com.dminvoicemanager.DM_Settings;
import com.dminvoicemanager.R;

public class DM_Merchandising_Information extends Activity {

	private static final String TAG = null;
	SQLiteDatabase mydb;
	String TagIdRetailer, merchantId, currentmerchCode,sumMerchDebCode,sumMerchCode;
	private String DBNAME = "dminvoicemanager.db";
	private String RETAILER_SUM = "FRetailScrSum";
	ListView lvtypedes;
	int SAVEMERCHTAG = 0;
	Button dmmerchandiseupdate;
	boolean isSelected;
	public void btnBack(View view) {
		Intent myIntent = new Intent(view.getContext(),
				DM_Retailer_Master.class);
		myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
				| Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivityForResult(myIntent, 0);
		finish();
	}

	public void CheckEvent(View view) {
		CheckBox des = (CheckBox) view;
		merchantId = des.getText().toString();
		Log.w(TAG, "merchantId:" + merchantId);
		Toast.makeText(getApplicationContext(), "merchantId :" + merchantId, 1)
				.show();
		updateCheckState();
	}

	private void updateCheckState() {
		// TODO Auto-generated method stub
		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		Cursor curCheckMerchState = null;
		try {
			String query = "SELECT * from FRetailScrSum Where SumMerchCode = '"
					+ merchantId + "'";
			curCheckMerchState = mydb.rawQuery(query, null);
		} catch (Exception e) {
		}
		if (curCheckMerchState != null) {
			if (curCheckMerchState.getCount() > 0) {
				while (curCheckMerchState.moveToNext()) {
					String merchRefId = curCheckMerchState.getString(0);
					currentmerchCode = curCheckMerchState.getString(2);
					mydb.delete(RETAILER_SUM, "retsum_id ='" + merchRefId
							+ "'", null);
					Log.v(TAG, "merchCode:" + currentmerchCode);
				}
			}
			else{
				try {
					ContentValues newMerchantAdd = new ContentValues();
					newMerchantAdd.put("DebCode", TagIdRetailer);
					newMerchantAdd.put("SumMerchCode", merchantId);
					newMerchantAdd.put("retSumaddDate",
							getCurrentTimeStringCustom().toString());
					mydb.insert(RETAILER_SUM, null, newMerchantAdd);
				} catch (Exception e) {
				}
			}
		}

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dm_merchandising_information);

		TextView txtHeading = (TextView) findViewById(R.id.txtHeading);
		txtHeading.setText("Merchandising Information");
		dmmerchandiseupdate = (Button) findViewById(R.id.dmmerchandiseupdate);
		callCurrentRetailer();
		Log.w(TAG, "TagIdRetailer:" + TagIdRetailer);

		DisplayMerchantList();
		
		
		dmmerchandiseupdate.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
				Cursor curReadMerch = null;
				try {
					String query = "SELECT * from FRetailScrSum Where SumMerchCode != '9999'";
					curReadMerch = mydb.rawQuery(query, null);
				} catch (Exception e) {
				}
				if (curReadMerch != null) {
					if (curReadMerch.getCount() > 0) {
						while (curReadMerch.moveToNext()) {
							String sumMerchid = curReadMerch.getString(0);
							sumMerchDebCode = curReadMerch.getString(1);
							sumMerchCode = curReadMerch.getString(2);
							
							updateDebMerch();
						}
					}
				}
			}

		});
		
	}

	protected void updateDebMerch() {
		// TODO Auto-generated method stub
		
	}

	private void callCurrentRetailer() {
		// TODO Auto-generated method stub
		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		Cursor curCurrentRetailer = null;
		try {
			String query = "SELECT * from FRetailScrSum";
			curCurrentRetailer = mydb.rawQuery(query, null);
		} catch (Exception e) {
		}
		if (curCurrentRetailer != null) {
			if (curCurrentRetailer.getCount() > 0) {
				while (curCurrentRetailer.moveToNext()) {
					TagIdRetailer = curCurrentRetailer.getString(1);
				}
			}
		}
	}

	private void DisplayMerchantList() {
		// TODO Auto-generated method stub
		lvtypedes = (ListView) findViewById(R.id.dmmerchandisedetlist);
		// create the grid item mapping
		String[] fromtrdes = new String[] { "coldes_1", "coldes_2" };
		int[] totrdes = new int[] { R.id.merchandisename, R.id.merchanavailable };
		// prepare the list of all records
		List<HashMap<String, String>> fillMapstrdes = new ArrayList<HashMap<String, String>>();

		SQLiteDatabase mydb;
		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		Cursor curListRetailer = null;
		try {
			String query = "SELECT MerchCode,MerchName from FMerch";
			curListRetailer = mydb.rawQuery(query, null);
		} catch (Exception e) {
		}
		if (curListRetailer != null) {
			if (curListRetailer.getCount() > 0) {
				while (curListRetailer.moveToNext()) {
					String strDebCode = curListRetailer.getString(0);
					String strDebName = curListRetailer.getString(1);

					HashMap<String, String> maptrdes = new HashMap<String, String>();
					maptrdes.put("coldes_1", strDebName);
					maptrdes.put("coldes_2", strDebCode);
					//maptrdes.put("coldes_2", );
					fillMapstrdes.add(maptrdes);

				}
				// fill in the grid_item layout
				SimpleAdapter adaptertrdes = new SimpleAdapter(this,
						fillMapstrdes,
						R.layout.dm_merchandising_information_raw, fromtrdes,
						totrdes);
				lvtypedes.setAdapter(adaptertrdes);
				DM_List_Expand_Helper.getListViewSize(lvtypedes);
			}
		}
		curListRetailer.close();
		mydb.close();
	}

	private String getCurrentTimeStringCustom() {
		// 2013-10-04 00:00:00.000
		Date dateNow = new Date();
		Calendar c = Calendar.getInstance();
		SimpleDateFormat ddMMMyyFormat = new SimpleDateFormat("dd/MM/yyyy");
		String date_to_string = ddMMMyyFormat.format(dateNow);
		return date_to_string;
	}
	@Override
	public void onBackPressed() {
		// Do Here what ever you want do on back press;
	}
}
